package org.mwielocha.jxcube.core;

import static org.mwielocha.jxcube.db.Database.agregadeQuery;
import static org.mwielocha.jxcube.db.Database.agregadeSearchAllFieldsQuery;
import static org.mwielocha.jxcube.db.Database.agregadeSearchFieldQuery;
import static org.mwielocha.jxcube.db.Database.lookupByAllFieldsQuery;
import static org.mwielocha.jxcube.db.Database.lookupByFieldQuery;
import static org.mwielocha.jxcube.db.Database.lookupQuery;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.mwielocha.jxcube.player.IPlayer;
import org.mwielocha.jxcube.player.IPlayerListener;
import org.mwielocha.jxcube.player.MPlayerFrontend;
import org.mwielocha.jxcube.tasks.PopulateDatabaseTask;
import org.mwielocha.jxcube.tasks.PopulateTableTask;
import org.mwielocha.jxcube.tasks.TaskManager;
import org.mwielocha.jxcube.ut.CustomSash;
import org.mwielocha.jxcube.ut.CustomTable;
import org.mwielocha.jxcube.ut.SimpleScaleBar;

public class JXCubeCoreApp implements IPlayerListener {

  protected Shell shell;
  
  private Table songsTable;
  private Menu songsTableMenu;
  
  private Table artistsTable;
  private Table albumsTable;
  private Label mainSongLabel;
  private FormData formData_1;
//  private Scale scaleVolume;
//  private Scale scalePosition;
  
  private Composite upperRightComposite, upperLeftComposite;
  
  private SimpleScaleBar scalePosition;
  private SimpleScaleBar scaleVolume;
  
  private FormData formData_2;
  private Label lowerSongLabel;
  
  private Text searchField;
  private boolean lockSearchField = false;
  
  private Label volumeNumberLabel;

  protected ResourceBundle res;
  protected TaskManager taskManager;
  
  protected IPlayer player;
  
  protected int currentSongIndex = -1;
  protected boolean lockPositon = false;
  
  /**
   * Launch the application.
   * 
   * @param args
   */
  public static void main(String[] args) {
    try {
      JXCubeCoreApp window = new JXCubeCoreApp();
      window.open();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Open the window.
   */
  public void open() {
    res = ResourceBundle.getBundle("properties/msg");
    taskManager = new TaskManager();
    Display display = new Display();
    shell = new Shell(display);
    createContents();
    try {
      taskManager.enqueue(new PopulateTableTask(agregadeQuery("artist"), artistsTable, false));
      taskManager.enqueue(new PopulateTableTask(agregadeQuery("album"), albumsTable, false));
      taskManager.enqueue(new PopulateTableTask(lookupQuery(), songsTable, true));
    } catch (Exception e) {
      e.printStackTrace();
    }
    player = new MPlayerFrontend();
    player.addPlayerListener(this);
    try {
      player.init();
    } catch (IOException e) {
      e.printStackTrace();
    }
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    System.out.println("Exit");
  }

  /**
   * Create contents of the window.
   */
  protected void createContents() {
    shell.setMinimumSize(new Point(300, 250));
    shell.setLayout(new swing2swt.layout.BorderLayout(0, 3));
    shell.setSize(768, 543);
    shell.setText(res.getString("mainWindow.title"));
    shell.addShellListener(new ShellAdapter() {
      @Override
      public void shellClosed(ShellEvent e) {
        close();
      }
    });
    
    {
    //  SashForm mainSashForm = new SashForm(shell, SWT.NONE);
      Composite sashParent = new Composite(shell, SWT.NONE);
      sashParent.setLayout(new FormLayout());
      
      {
        upperLeftComposite = new Composite(sashParent, SWT.BORDER);
        RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
        rowLayout.fill = true;
        upperLeftComposite.setLayout(rowLayout);
        {
          Button libraryButton = new Button(upperLeftComposite, SWT.NONE);
          libraryButton.setText(res.getString("general.library"));
          libraryButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              try {
                clearTables();
                taskManager.enqueue(new PopulateTableTask(agregadeQuery("artist"), artistsTable, false));
                taskManager.enqueue(new PopulateTableTask(agregadeQuery("album"), albumsTable, false));
                taskManager.enqueue(new PopulateTableTask(lookupQuery(), songsTable, true));
              } catch (Exception ex) {
                ex.printStackTrace();
              }
            }
          });
        }
        {
          searchField = new Text(upperLeftComposite, SWT.BORDER);
          searchField.setLayoutData(new RowData(120, SWT.DEFAULT));
          searchField.setText(res.getString("general.search") + "...");
          searchField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent arg0) {
              lockSearchField = true;
              searchField.setText("");
              lockSearchField = false;
            }
            
            @Override
            public void focusLost(FocusEvent arg0) {
              lockSearchField = true;
              searchField.setText(res.getString("general.search") + "...");
              lockSearchField = false;
            }
          });
          
          searchField.addModifyListener(new ModifyListener() {
            
            @Override
            public void modifyText(ModifyEvent arg0) {
              if(!lockSearchField) {
                try {
                  String value = searchField.getText();
                  System.out.println(searchField.getText());
                  taskManager.executeImmediate(
                      new PopulateTableTask(agregadeSearchAllFieldsQuery("artist", value), artistsTable, false));
                  taskManager.enqueue(
                      new PopulateTableTask(agregadeSearchAllFieldsQuery("album", value), albumsTable, false));
                  taskManager.enqueue(new PopulateTableTask(lookupByAllFieldsQuery(value), songsTable, true));
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
            }
          });
        }
      }
      {
        upperRightComposite = new Composite(sashParent, SWT.NONE);
        upperRightComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
        {
          SashForm upperSashForm = new SashForm(upperRightComposite, SWT.NONE);
          upperSashForm.setOrientation(SWT.VERTICAL);
          {
            Composite upperTableComposite = new Composite(upperSashForm,
                SWT.NONE);
            upperTableComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
            {
              SashForm upperTableSashForm = new SashForm(upperTableComposite,
                  SWT.NONE);
              {
                artistsTable = new Table(upperTableSashForm, SWT.BORDER
                    | SWT.FULL_SELECTION);
                artistsTable.setHeaderVisible(true);
                artistsTable.setLinesVisible(true);
                {
                  TableColumn tableColumn = new TableColumn(artistsTable,
                      SWT.NONE);
                  tableColumn.setWidth(240);
                  tableColumn.setText(res.getString("mainWidowm.artistsTable.column"));
                }
                {
                  TableColumn tableColumn = new TableColumn(artistsTable, SWT.RIGHT);
                  tableColumn.setWidth(40);
                  tableColumn.setText("#");
                }
                artistsTable.addSelectionListener(new SelectionAdapter() {
                  @Override
                  public void widgetSelected(SelectionEvent arg0) {
                    int idx = artistsTable.getSelectionIndex();
                    if(idx >= 0) {
                      try {
                        String value = artistsTable.getItem(idx).getText(0);
                        taskManager.executeImmediate(
                            new PopulateTableTask(agregadeSearchFieldQuery("artist", "artist", value), artistsTable, false));
                        taskManager.enqueue(
                            new PopulateTableTask(agregadeSearchFieldQuery("album", "artist", value), albumsTable, false));
                        taskManager.enqueue(new PopulateTableTask(lookupByFieldQuery("artist", value), songsTable, true));
                      } catch (Exception e) {
                        e.printStackTrace();
                      }
                    }
                  }
                });
              }
              {
                albumsTable = new Table(upperTableSashForm, SWT.BORDER | SWT.FULL_SELECTION);
                albumsTable.setHeaderVisible(true);
                albumsTable.setLinesVisible(true);
                {
                  TableColumn tableColumn = new TableColumn(albumsTable,
                      SWT.NONE);
                  tableColumn.setWidth(240);
                  tableColumn.setText(res.getString("mainWindow.albumsTable.column"));
                }
                {
                  TableColumn tableColumn = new TableColumn(albumsTable, SWT.RIGHT);
                  tableColumn.setWidth(40);
                  tableColumn.setText("#");
                }
                albumsTable.addSelectionListener(new SelectionAdapter() {
                  @Override
                  public void widgetSelected(SelectionEvent arg0) {
                    int idx = albumsTable.getSelectionIndex();
                    if(idx >= 0) {
                      try {
                        String value = albumsTable.getItem(idx).getText(0);
                        taskManager.executeImmediate(
                            new PopulateTableTask(agregadeSearchFieldQuery("artist", "album", value), artistsTable, false));
                        taskManager.enqueue(
                            new PopulateTableTask(agregadeSearchFieldQuery("album", "album", value), albumsTable, false));
                        taskManager.enqueue(new PopulateTableTask(lookupByFieldQuery("album", value), songsTable, true));
                      } catch (Exception e) {
                        e.printStackTrace();
                      }
                    }
                  }
                });
              }
              upperTableSashForm.setWeights(new int[] { 296, 317 });
            }
          }
          {
            Composite lowerTableComposite = new Composite(upperSashForm,
                SWT.NONE);
            lowerTableComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
            {
              songsTable = new Table(lowerTableComposite, SWT.NONE | SWT.SINGLE | SWT.FULL_SELECTION);
              songsTable.setHeaderVisible(true);
//              songsTable.setLinesVisible(true);
              {
                TableColumn tableColumn = new TableColumn(songsTable, SWT.NONE);
                tableColumn.setWidth(60);
                tableColumn.setText(res.getString("mainWindow.mainTable.trackColumn"));
              }
              {
                TableColumn tableColumn = new TableColumn(songsTable, SWT.NONE);
                tableColumn.setWidth(240);
                tableColumn.setText(res.getString("mainWindow.mainTable.titleColumn"));
              }
              {
                TableColumn tableColumn = new TableColumn(songsTable, SWT.NONE);
                tableColumn.setWidth(240);
                tableColumn.setText(res.getString("mainWindow.mainTable.artistColumn"));
              }
              {
                TableColumn tableColumn = new TableColumn(songsTable, SWT.NONE);
                tableColumn.setWidth(240);
                tableColumn.setText(res.getString("mainWindow.mainTable.albumColumn"));
              }
              {
                TableColumn tableColumn = new TableColumn(songsTable, SWT.NONE);
                tableColumn.setWidth(120);
                tableColumn.setText(res.getString("mainWindow.mainTable.genreColumn"));
              }
              {
                TableColumn tableColumn = new TableColumn(songsTable, SWT.NONE | SWT.RIGHT);
                tableColumn.setWidth(80);
                tableColumn.setText(res.getString("mainWindow.mainTable.timeColumn"));
              }
              {
                TableColumn tableColumn = new TableColumn(songsTable, SWT.NONE);
                tableColumn.setWidth(0);
                tableColumn.setText("path");
              }
              {
                songsTableMenu = new Menu(songsTable);
                songsTable.setMenu(songsTableMenu);
                {
                  MenuItem playSongsTableMenuItem = new MenuItem(songsTableMenu, SWT.NONE);
                  playSongsTableMenuItem.setText(res.getString("mainWindow.mainTable.menu.itemPlay"));
                  playSongsTableMenuItem.addSelectionListener(new SelectionAdapter() {
                    @Override
                    public void widgetSelected(SelectionEvent e) {
                      play(songsTable.getSelectionIndex());
                    }
                  });
                }
                {
                  MenuItem infoSongsTableMenuItem = new MenuItem(songsTableMenu, SWT.NONE);
                  infoSongsTableMenuItem.setText(res.getString("mainWindow.mainTable.menu.itemInfo"));
                  infoSongsTableMenuItem.addSelectionListener(new SelectionAdapter() {
                    @Override
                    public void widgetSelected(SelectionEvent e) {
                      new TrackInfoDialog(shell, SWT.DIALOG_TRIM).open(songsTable.getSelection()[0]);
                    }
                  });
                }
                songsTable.addMouseListener(new MouseAdapter() {
                  @Override
                  public void mouseDown(MouseEvent e) {
                    if(e.button == 2) {
                      songsTableMenu.setLocation(e.x, e.y);
                      songsTable.setVisible(true);
                    }
                  }
                });
              }
            }
          }
          upperSashForm.setWeights(new int[] { 93, 161 });
        }
      }
      //mainSashForm.setWeights(new int[] { 10, 100 });
      CustomSash customSash = new CustomSash(sashParent, upperLeftComposite, upperRightComposite);
    }
    {
      Composite lowerComposite = new Composite(shell, SWT.BORDER);
      lowerComposite.setLayout(new swing2swt.layout.BorderLayout(0, 0));
      lowerComposite.setLayoutData(swing2swt.layout.BorderLayout.SOUTH);
      {
        Composite playerControlComposite = new Composite(lowerComposite,
            SWT.NONE);
        playerControlComposite
            .setLayoutData(swing2swt.layout.BorderLayout.WEST);
        GridLayout gridLayout = new GridLayout(2, true);
        playerControlComposite.setLayout(gridLayout);
        {
          Button btnPlay = new Button(playerControlComposite, SWT.NONE);
          btnPlay.setImage(new Image(shell.getDisplay(), "img/play_dark_uf.png"));
          btnPlay.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              int idx = songsTable.getSelectionIndex();
              if(idx >= 0) {
                play(idx);
              }
            }
          });
        }
        {
          Button btnStop = new Button(playerControlComposite, SWT.NONE);
          btnStop.setImage(new Image(shell.getDisplay(), "img/stop_dark_uf.png"));
          btnStop.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              try {
                player.stop();
              } catch (Exception ex) {
                ex.printStackTrace();
              }       
            }
          });
        }
        {
          Button btnPause = new Button(playerControlComposite, SWT.NONE);
          btnPause.setImage(new Image(shell.getDisplay(), "img/pause_dark_uf.png"));
        }
        {
          Button btnShuffle = new Button(playerControlComposite, SWT.NONE);
          btnShuffle.setImage(new Image(shell.getDisplay(), "img/random_dark_uf.png"));
        }
      }
      {
        Composite playerMainComposite = new Composite(lowerComposite, SWT.NONE);
        playerMainComposite.setLayout(new swing2swt.layout.BorderLayout(0, 0));
        playerMainComposite.setLayoutData(swing2swt.layout.BorderLayout.CENTER);
        {
          Composite upperPlayerComposite = new Composite(playerMainComposite,
              SWT.NONE);
          upperPlayerComposite
              .setLayoutData(swing2swt.layout.BorderLayout.CENTER);
          upperPlayerComposite.setLayout(new FormLayout());
          {
            mainSongLabel = new Label(upperPlayerComposite, SWT.NONE);
            mainSongLabel.setFont(new Font(Display.getDefault(),"Sans", 12,
                SWT.BOLD));
            {
              formData_1 = new FormData();
              formData_1.left = new FormAttachment(0, 10);
              mainSongLabel.setLayoutData(formData_1);
            }
            mainSongLabel.setText("New Label");
          }
          {
            lowerSongLabel = new Label(upperPlayerComposite, SWT.NONE);
            formData_1.right = new FormAttachment(lowerSongLabel, 0, SWT.RIGHT);
            formData_1.bottom = new FormAttachment(lowerSongLabel, -6);
            {
              FormData formData = new FormData();
              formData.right = new FormAttachment(0, 539);
              formData.top = new FormAttachment(0, 35);
              formData.left = new FormAttachment(0, 10);
              formData.height = 20;
              lowerSongLabel.setLayoutData(formData);
            }
            lowerSongLabel.setText("New Label");
          }
          {
            scaleVolume = new SimpleScaleBar(upperPlayerComposite, 5, 5);
            scaleVolume.setMinimum(0);
            scaleVolume.setMaximum(100);
            scaleVolume.addMouseListener(new MouseAdapter() {
              @Override
              public void mouseUp(MouseEvent e) {
                try {
                  player.setVolume(scaleVolume.getSelection());
                } catch (Exception ex) {
                  ex.printStackTrace();
                }
              }
            });
            
            scaleVolume.addPropertyChangeListener(new PropertyChangeListener() {
              public void propertyChange(PropertyChangeEvent evt) {
                volumeNumberLabel.setText(evt.getNewValue() + "");
              }
            });
            
            {
              formData_2 = new FormData();
              formData_2.bottom = new FormAttachment(100, -13);
              formData_2.width = 79;
              scaleVolume.setLayoutData(formData_2);
            }
          }
          {
            Label volumeLabel = new Label(upperPlayerComposite, SWT.NONE);
            {
              FormData formData = new FormData();
              formData.bottom = new FormAttachment(scaleVolume.getComposite());
              formData.left = new FormAttachment(scaleVolume.getComposite(), 0, SWT.LEFT);
              volumeLabel.setLayoutData(formData);
            }
            volumeLabel.setText(res.getString("general.volume"));
          }
          {
            volumeNumberLabel = new Label(upperPlayerComposite, SWT.NONE);
            formData_2.right = new FormAttachment(100, -31);
            {
              FormData formData = new FormData();
              formData.top = new FormAttachment(scaleVolume.getComposite(), 0, SWT.TOP);
              formData.left = new FormAttachment(scaleVolume.getComposite(), 6);
              volumeNumberLabel.setLayoutData(formData);
            }
            volumeNumberLabel.setText("100");
          }
        }
        {
          Composite lowerSupportComposite = new Composite(playerMainComposite,
              SWT.NONE);
          lowerSupportComposite.setLayout(new swing2swt.layout.BorderLayout(0,
              0));
          lowerSupportComposite
              .setLayoutData(swing2swt.layout.BorderLayout.SOUTH);
          {
            scalePosition = new SimpleScaleBar(lowerSupportComposite, 5, 5);
            scalePosition.setLayoutData(swing2swt.layout.BorderLayout.CENTER);
            scalePosition.addMouseListener(new MouseAdapter() {
              @Override
              public void mouseUp(MouseEvent e) {
                try {
                  player.setPosition((double) (scalePosition.getSelection() / 10));
                  lockPositon = false;
                } catch (IOException e1) {
                  e1.printStackTrace();
                }
              }
              
              @Override
              public void mouseDown(MouseEvent e) {
                lockPositon = true;
              }
            });
            
            
            
          }
          {
            Composite leftSupportComposite = new Composite(
                lowerSupportComposite, SWT.NONE);
            GridLayout gridLayout = new GridLayout(1, false);
            gridLayout.marginHeight = 3;
            gridLayout.marginWidth = 3;
            leftSupportComposite.setLayout(gridLayout);
            leftSupportComposite
                .setLayoutData(swing2swt.layout.BorderLayout.WEST);
            {
              Label currentTimeLabel = new Label(leftSupportComposite, SWT.NONE);
              currentTimeLabel.setText("1:25");
            }
          }
          {
            Composite rightSupportComposite = new Composite(
                lowerSupportComposite, SWT.NONE);
            GridLayout gridLayout = new GridLayout(1, false);
            gridLayout.marginWidth = 3;
            gridLayout.marginHeight = 3;
            rightSupportComposite.setLayout(gridLayout);
            rightSupportComposite
                .setLayoutData(swing2swt.layout.BorderLayout.EAST);
            {
              Label totalTimeLabel = new Label(rightSupportComposite, SWT.NONE);
              totalTimeLabel.setText("7:30");
            }
          }
        }
      }
    }
    {
      Menu menuBar = new Menu(shell, SWT.BAR);
      shell.setMenuBar(menuBar);
      {
        MenuItem fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
        fileMenuHeader.setText(res.getString("mainWindow.menu.file"));
        {
          Menu fileMenu = new Menu(fileMenuHeader);
          fileMenuHeader.setMenu(fileMenu);
          {
            MenuItem exitMenuItem = new MenuItem(fileMenu, SWT.NONE);
            exitMenuItem.setText(res.getString("mainWindow.menu.exit"));
            exitMenuItem.addSelectionListener(new SelectionAdapter() {
              @Override
              public void widgetSelected(SelectionEvent e) {
                close();
              }
            });
          }
        }
      }
      {
        MenuItem libraryMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
        libraryMenuHeader.setText(res.getString("general.library"));
        {
          Menu menu = new Menu(libraryMenuHeader);
          libraryMenuHeader.setMenu(menu);
          {
            MenuItem synchMenuItem = new MenuItem(menu, SWT.NONE);
            synchMenuItem.setText(res.getString("mainWindow.menu.synchronizePaths"));
            synchMenuItem.addSelectionListener(new SelectionAdapter() {
              @Override
              public void widgetSelected(SelectionEvent e) {
                new SynchDialog(shell, SWT.NONE).open();
              }
            });
          }
          {
            MenuItem synchDialogMenuItem = new MenuItem(menu, SWT.NONE);
            synchDialogMenuItem.setText(res.getString("mainWindow.menu.synchronize"));
            synchDialogMenuItem.addSelectionListener(new SelectionAdapter() {
              @Override
              public void widgetSelected(SelectionEvent e) {
                taskManager.enqueue(new PopulateDatabaseTask(""));
              }
            });
          }
        }
      }
    }

  }
  
  protected void clearTables() {
    songsTable.removeAll();
    artistsTable.removeAll();
    albumsTable.removeAll();
  }
  
  protected void close() {
    try {
      player.close();
      shell.close();
      System.exit(0);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  
  protected void play(int idx) {
    currentSongIndex = idx;
    TableItem item = songsTable.getItem(idx);
    try {
      player.play(item.getText(6));
    } catch (IOException e) {
      e.printStackTrace();
    }
    mainSongLabel.setText(item.getText(1));
    lowerSongLabel.setText(item.getText(2) + " : " + item.getText(3));
  }
  
  /* =-=-=-=-=-=-=-=-=-=-=-=-=-= IPLayerListener =-=-=-=-=-=-=-=-=-=-=-=-=-=- */
  
  @Override
  public void playEvent() {
    // TODO Auto-generated method stub
    
  }
  
  @Override
  public void positionChangeEvent(double position, double lastPosition) {
    final int positionInt = (int) (position * 10);
    final int lastPositionInt = (int) (lastPosition * 10);
    if(!lockPositon) {
      Display.getDefault().asyncExec(new Runnable() {
        
        @Override
        public void run() {
          System.out.println(positionInt + " :  " + lastPositionInt);
          scalePosition.setMaximum(lastPositionInt);
          scalePosition.setMinimum(0);
          scalePosition.setSelection(positionInt);
        }
      });
    }
  }
  
  @Override
  public void stopEvent() {
    // TODO Auto-generated method stub
    
  }
  
  public void trackFinishedEvent() {
    System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    Display.getDefault().asyncExec(new Runnable() {
      
      @Override
      public void run() {
        if(currentSongIndex < songsTable.getItemCount()) {
          play(currentSongIndex + 1);
        }
      }
    });
  };
  
  @Override
  public void volumeChangedEvent(final int volume) {
    
  }
}
